export const getSearchParams = (searchText) => {
  const searchArray = searchText.split('&');
  const result = {};
  searchArray.map(item => {
    const temp = item.split('=');
    result[temp[0]] = temp[1];
  });
  return result;
}

export const getUrlParams = () => {
  const searchText = window.location.search.substring(1);
  return getSearchParams(searchText)
}

export const getHashParams = () => {
  const hash = window.location.hash;
  const searchText = hash.substring(hash.indexOf('?') + 1);
  return getSearchParams(searchText)
}

/**
 * 在body中引入外部script
 * @param {string} url script链接
 * @param {string} id script标签id
 * @param {function} cb 回调函数
 */
export function appendScript(url, id) {
  return new Promise((resolve) => {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    script.id = id || `js-${new Date().getTime()}`;
    script.onload = () => {
      resolve();
    };
    document.querySelector('body').appendChild(script);
  });
}
